VFG.ERoadMark=function(viewer, options) {
	this.viewer = viewer;
	this.options = options;
	this.parentId= options.parentId||'';
	this.id= options.id;
	this.name= options.name||'';
	this.code= options.code||'';
	this.type= options.type||'';
	this.thing=options.thing||{};
	this.attribute=options.attribute||{};
	this.propertys=options.propertys||[];
	this.init();
}
VFG.ERoadMark.prototype.init = function() {
	let _this=this;
	this.entity= this.viewer.entities.add({
		id:this.id,
		name:this.name,
		code:this.code,
		type:this.type,
		position:this.getCenter(),
        rectangle : {
	       coordinates :Cesium.Rectangle.fromCartesianArray(_this.thing.coordinates),
		   rotation: Cesium.Math.toRadians(_this.thing.angle||0),
		   stRotation: Cesium.Math.toRadians(_this.thing.angle||0),
	       material :	_this.getMaterial(),
	       heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
	    }
   });
};

VFG.ERoadMark.prototype.updateName = function(name) {
	this.name=name;
	if(this.entity){
		this.entity.name=this.name;
		this.entity.rectangle.material=this.getMaterial();
	}
}

VFG.ERoadMark.prototype.updateCode = function(code) {
	this.code=code;
	if(this.entity){
		this.entity.code=this.code;
	}
}

VFG.ERoadMark.prototype.updateFont = function(font) {
	this.thing.font=font;
	if(this.entity){
		this.entity.rectangle.material=this.getMaterial();
	}
}

VFG.ERoadMark.prototype.updateFontSize = function(fontSize) {
	this.thing.fontSize=fontSize;
	if(this.entity){
		this.entity.rectangle.material=this.getMaterial();
	}
}

VFG.ERoadMark.prototype.updateFontColor = function(fontColor) {
	this.thing.fontColor=fontColor;
	if(this.entity){
		this.entity.rectangle.material=this.getMaterial();
	}
}

VFG.ERoadMark.prototype.updateAngle = function(angle) {
	var _this = this;
	this.thing.angle=angle;
	if(this.entity){
		this.entity.rectangle.rotation=new Cesium.CallbackProperty(function () {
			return Cesium.Math.toRadians(_this.thing.angle||0);
		}, false);
		this.entity.rectangle.stRotation=new Cesium.CallbackProperty(function () {
			return Cesium.Math.toRadians(_this.thing.angle||0);
		}, false);
	}
}

VFG.ERoadMark.prototype.getMaterial = function() {
	var _this = this;
	return new Cesium.ImageMaterialProperty({
        image: _this.drawText(),
        transparent: true,
        color: Cesium.Color.WHITE
    });
}

VFG.ERoadMark.prototype.drawText = function() {
	let _this=this;
/*    let text = this.name;
    var c = document.createElement("canvas");
    let d = (""+text+"").length * ( this.thing.fontSize||14);
    c.width = d;
    c.height = ( this.thing.fontSize||20);
    let ctx = c.getContext("2d");
    ctx.fillStyle = (( this.thing.fontColor||"rgb(255,255,255)"));
    ctx.font = "bold " +( this.thing.fontSize||20)+"px sans-serif";
    ctx.textBaseline = 'hanging'; */
    
	var canvas = document.createElement("canvas");
	let context = canvas.getContext("2d");
	
	context.width = Cesium.Rectangle.computeWidth  (Cesium.Rectangle.fromCartesianArray(_this.thing.coordinates));
	context.height=Cesium.Rectangle.computeHeight (Cesium.Rectangle.fromCartesianArray(_this.thing.coordinates))
	 
    context.fillStyle = "red";  // 填充颜色为红色
    context.strokeStyle = "blue";  // 画笔的颜色
    context.font = "28px sans-serif";
    context.textBaseline = 'hanging';
    context.fillText(_this.name,0, 0);
    
    return  canvas
};

VFG.ERoadMark.prototype.getCenter = function() {
	var rectangle = this.thing.coordinates;
	var center = Cesium.Rectangle.center(rectangle);
	var adre = Cesium.Cartographic.toCartesian(center);
	return adre;
}

VFG.ERoadMark.prototype.updateCoordinates = function(coordinates,isDynamic) {
	let _this=this;
	if(coordinates){
		this.thing.coordinates=coordinates;
		if(this.entity){
			if(isDynamic){
				this.entity.rectangle.coordinates=new Cesium.CallbackProperty(function (time) {
					return Cesium.Rectangle.fromCartesianArray(_this.thing.coordinates)
				}, false)
			}else{
				this.entity.rectangle.coordinates=Cesium.Rectangle.fromCartesianArray(this.thing.coordinates)
			}
		}
	}
}

VFG.ERoadMark.prototype.destroy = function() {
	let _this=this;
	if(this.entity){
		this.viewer.entities.remove(this.entity);
	}
	delete this.entity
	delete this.options
	delete this.thing
    delete this.viewer
	return Cesium.destroyObject(this);
};
